#!/usr/bin/php
<?php
#alp: alpine-backup Proto6
#(C) Copyright 2007-2011 Nicholas Paun. All Rights Reserved.
/*Knut 061*/


define('OPTIONS','-av');
define('DATE_FORMAT','Y-m-d');
define('INCLUDE_PATH','/usr/lib/alpine-backup/');
define('CONFIG_PATH','/etc/alpine-backup/');

if (@$argv[1] == "-h")
 {
  echo("alpine-backup Version: 0.61\n");
  echo("Syntax: ".$argv[0]." <server> <item>\n");
  echo("Copyright 2007-2011 Nicholas Paun\n");
  die(0);
 }

if($argc != 3)
 {
  echo("Invalid number of arguments. Try ".$argv[0]." -h for more info.\n"); 
  die(3);
 }

$conf = parse_ini_file(CONFIG_PATH.$argv[1],TRUE);
$item = $argv[2];

if (!isset($conf["$item"]))
 {
  echo("* Cannot backup $item on ".$argv[1].": Invalid backup item.\n");
  die(3);
 }

echo("* Backing up $item on ".$argv[1].".\n");

function mount_roo($data,$umount=FALSE)
 {
  @list($type,$src,$point) = explode(",",$data);
  list($user,$host) = explode("@",$src);
  list($server,$volume) = explode(":",$host);

  require_once(INCLUDE_PATH."$type-roo"); //Loads by filesystem
  
  if (!$umount) 
   {
    $return = mountfs($user,$server,$volume,$point);
    if (!$return)
     {
      echo("* Failed to mount $volume: mount script unsuccessful.\n");
      die(2);
     }
    }
  else
  {
   $return = umountfs($user,$server,$volume,$point);
   if (!$return) echo("* WARNING: Could not unmount $volume.\n");
  }
 }

function delete_old($dest,$keep)
 {
  $time = strtotime($keep); #Accept values such as '30 days ago'
  $old_date = date(DATE_FORMAT,$time);

  if (file_exists("$dest/$old_date")) #Prevent accidental deletion of entire backup directory
  {
   exec("rm -r $dest/$old_date",$out,$return);
  }
  if (!isset($return) || $return != 0)
   {
    echo("* Failed to delete old backups from $keep.\n");
   }
 }

function link_check($dest,$name)
 {
  if (is_link("$dest/current"))
   {
    $link = @readlink("$dest/current"); #Find where the link is pointing to
    if (!file_exists($link))
     {
      echo("* Backup Failure ($name): The current symlink points to a non-existent location.\n");
      die(4);
     }
   }
 }


function wrapper($conf,$item)
 {
  $date = date(DATE_FORMAT);
  extract($conf["$item"]); #Simplifies variables
  if (isset($mount))
   {
    mount_roo($mount); #Mount
   }
  passthru("mkdir -p $dest"); #Auto-create destination directory structure (mkdir() creates one level)
  link_check($dest,$item); #Check for invalid link-dests

  backup(@$opt,$source,$dest,$date,$item);

  if (isset($mount))
  {
   mount_roo($mount,TRUE); #Unmount
  }
 if (isset($delete_after))
  {
   delete_old($dest,$delete_after); //Remove old backups
  }
 }

function backup($opt,$src,$dest,$date,$name)
 {
  $_opt = OPTIONS;
  passthru("rsync $_opt $opt --link-dest $dest/current $src $dest/$date",$return);
  if ($return != 0)
   {
    echo("* Backup Failure ($name): rsync did not finish successfully.\n");
    die(1);
   }  
  unlink("$dest/current");
  symlink("$dest/$date","$dest/current");
  echo("* Backup Successful: $name\n");
 }

wrapper($conf,$item);

